package pl.edu.agh.to;

import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

abstract class OrganizerDialog extends AlertDialog {
	protected final TemplateActivity activity;
	private final DBAccess dBAccess;

	/**
	 * @return the dBAccess
	 */
	public DBAccess getdBAccess() {
		return dBAccess;
	}

	/**
	 * @return the activity
	 */
	public TemplateActivity getActivity() {
		return activity;
	}

	OrganizerDialog(TemplateActivity activity) {
		super(activity);
		this.activity = activity;
		dBAccess = DBAccess.instance(activity);
		setCancelable(false);
		setMessage(getBaseMessage());
		setButton(BUTTON_POSITIVE,
				getContext().getResources().getText(R.string.yes),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						action();
					}
				});
		setButton(BUTTON_NEGATIVE,
				getContext().getResources().getText(R.string.no),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
	}

	abstract protected void action();

	abstract protected String getBaseMessage();

	abstract public void prepare();

}

abstract class AddDialog extends OrganizerDialog implements TextWatcher {
	private EditText upperTextEdit;
	private EditText lowerTextEdit;

	AddDialog(TemplateActivity activity) {
		super(activity);
		LayoutInflater factory = LayoutInflater.from(getActivity());
		View view = factory.inflate(layoutId(), null);
		setView(view);

	}

	/**
	 * @return
	 */
	protected EditText getLowerTextEdit() {
		if (lowerTextEdit == null) {
			lowerTextEdit = ((EditText) findViewById(lowerTextEditId()));
		}
		return lowerTextEdit;
	}

	/**
	 * @return
	 */
	protected EditText getUpperTextEdit() {
		if (upperTextEdit == null) {
			upperTextEdit = ((EditText) findViewById(upperTextEditId()));
		}
		return upperTextEdit;
	}

	abstract protected int upperTextEditId();

	abstract protected int lowerTextEditId();

	@Override
	public void prepare() {
		getUpperTextEdit().removeTextChangedListener(this);
		getLowerTextEdit().removeTextChangedListener(this);
		prepareDialog();
		getUpperTextEdit().addTextChangedListener(this);
		getLowerTextEdit().addTextChangedListener(this);
		getUpperTextEdit().requestFocus();
	}

	@Override
	public void afterTextChanged(Editable arg0) {
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		if (enableButton()) {
			getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
		} else {
			// TODO consider some more verification
			getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	abstract protected void prepareDialog();

	abstract protected boolean enableButton();

	abstract protected int layoutId();
}

abstract class RemoveDialog extends OrganizerDialog {
	private String spinnerItem;

	public String getSpinnerItem() {
		return spinnerItem;
	}

	@Override
	public void prepare() {
		setSpinnerSelectedItem();
		setMessage(getBaseMessage() + getSpinnerItem());
	}

	/**
	 * 
	 */
	protected void setSpinnerSelectedItem() {
		this.spinnerItem = getActivity().getSpinnerSelectedItem();
	}

	@Override
	protected String getBaseMessage() {
		return getContext().getResources().getText(getMessageId()).toString();
	}

	RemoveDialog(TemplateActivity activity) {
		super(activity);
	}

	abstract protected int getMessageId();

}

abstract class AddToDialog extends RemoveDialog {

	AddToDialog(TemplateActivity activity) {
		super(activity);
		LayoutInflater factory = LayoutInflater.from(activity);
		View view = factory.inflate(getViewId(), null);
		setView(view);
	}

	abstract protected int getViewId();

	abstract protected List<Object> getSpinnerContent();

	abstract protected int getDialogSpinnerId();

	@Override
	public void prepare() {
		super.prepare();
		ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(getActivity(),
				android.R.layout.simple_spinner_item, getSpinnerContent());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		getDialogSpinner().setAdapter(adapter);
	}

	Spinner getDialogSpinner() {
		return (Spinner) findViewById(getDialogSpinnerId());
	}

	String getDialogSpinnerItem() {
		if (getDialogSpinner().getSelectedItem() != null) {
			return getDialogSpinner().getSelectedItem().toString();
		}
		return null;
	}

}
